package com.pzhu.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.entity.*;
import com.pzhu.service.*;
import com.pzhu.until.JwtTokenUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("api/car")

public class ShoppingCarController<T> {
    @Autowired
    private ShoppingcarService shoppingcarService;

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private HotelService hotelService;
    @Autowired

    private PhotographService photographService;
    @Autowired
    private OrderService orderService;

    /**
     * 添加购物车
     *
     * @param request
     * @param id
     * @param type
     * @return
     */
    @PostMapping
    public Result add(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("type") Integer type) {
        Integer user = JwtTokenUntil.getClaim(request, "loginUser").asInt();
        System.out.println(user);

        Shoppingcar shoppingcar = new Shoppingcar(null, user, id, 1, type, null);

        boolean save = shoppingcarService.save(shoppingcar);

        return new Result(Code.success, "", save);


    }

    /**
     * 查询购物车
     *
     * @param request
     * @param current
     * @param size
     * @return
     */
    @GetMapping
    public Result select(HttpServletRequest request, @RequestParam("current") Integer current, @RequestParam("size") Integer size) {

        Integer user = JwtTokenUntil.getClaim(request, "loginUser").asInt();
        Page<Shoppingcar> shoppingcarPage = new Page<>(current, size);

        LambdaQueryWrapper<Shoppingcar> shoppingcarLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingcarLambdaQueryWrapper.eq(Shoppingcar::getUserid, user);


        Page<Shoppingcar> page = shoppingcarService.page(shoppingcarPage, shoppingcarLambdaQueryWrapper);

        for (Shoppingcar e : page.getRecords()) {
            if (e.getType() == 1) {
                Goods byId = goodsService.getById(e.getId());
                e.setData(byId);
            }

            if (e.getType() == 2) {
                Hotel byId = hotelService.getById(e.getId());
                e.setData(byId);

            }

            if (e.getType() == 3) {
                Photograph byId = photographService.getById(e.getId());
                e.setData(byId);
            }
        }


        return new Result(Code.success, "", page);
    }

    /**
     * 修改购物车
     *
     * @param request
     * @param shoppingcar
     * @return
     */

    @PutMapping
    public Result upadte(HttpServletRequest request, @RequestBody List<Shoppingcar> shoppingcar) {
        Integer user = JwtTokenUntil.getClaim(request, "loginUser").asInt();
        LambdaUpdateWrapper<Shoppingcar> shoppingcarLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        shoppingcarLambdaUpdateWrapper.eq(Shoppingcar::getUserid, user);
        shoppingcarLambdaUpdateWrapper.eq(Shoppingcar::getCarid, shoppingcar.get(0).getCarid());
        shoppingcarLambdaUpdateWrapper.set(Shoppingcar::getNumber, shoppingcar.get(0).getNumber());


        boolean update = shoppingcarService.update(shoppingcarLambdaUpdateWrapper);
        return new Result(Code.success, "", update);


    }

    /**
     * 支付 插入订单
     *
     * @param request
     * @param shoppingcar
     * @return
     */
    @PostMapping("pay")
    public Result insertorder(HttpServletRequest request, @RequestBody List<Shoppingcar> shoppingcar) {

        Sequence sequence = new Sequence(1, 1);
        Date date = new Date();
        Integer user = JwtTokenUntil.getClaim(request, "loginUser").asInt();
        List<ORder> list = new ArrayList<>();
        shoppingcar.forEach(e -> {
            if (e.getType() == 1) {
                Goods goods = JSON.parseObject(JSON.toJSONString(e.getData()), Goods.class);
                ORder oRder = new ORder(null, sequence.nextId(), goods.getGoodsName(), e.getNumber(), user, null, null, goods.getGoodsPrice(), date, goods.getGoodsBusinessId(), 0, e.getType(), 0);

                list.add(oRder);
            }


            if (e.getType() == 2) {
//                Hotel  hotel= (Hotel) e.getData();

                Hotel hotel = JSON.parseObject(JSON.toJSONString(e.getData()), Hotel.class);

                ORder oRder = new ORder(null, sequence.nextId(), hotel.getHotelName(), e.getNumber(), user, null, null, BigDecimal.valueOf(1), date, hotel.getHotelBusinessId(), 0, e.getType(), 0);

                list.add(oRder);
            }
            if (e.getType() == 3) {
                Photograph photograph = JSON.parseObject(JSON.toJSONString(e.getData()), Photograph.class);
                ORder oRder = new ORder(null, sequence.nextId(), photograph.getPName(), e.getNumber(), user, null, null, photograph.getPPrice(), date, photograph.getPBusinessId(), 0, e.getType(), 0);

                list.add(oRder);
            }
        });
        boolean b = orderService.saveBatch(list);
        if (b) shoppingcarService.removeBatchByIds(shoppingcar);
        return new Result(Code.success, "", b);
    }

    /**
     * 删除订单
     *
     * @param shoppingcar
     * @return
     */
    @DeleteMapping
    public Result delte(@RequestBody List<Shoppingcar> shoppingcar) {

        boolean b = shoppingcarService.removeBatchByIds(shoppingcar);
        return new Result(Code.success, "", b);
    }


}
